﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using MossmanMill.Properties;

namespace MossmanMill.View
{
    class Scrollbar
    {
        private Rectangle rect, up, down, bar;
        private int max;
        public int offset;
        private bool drag;
        private Point[] upLines, downLines;
        private float ratio;

        public Scrollbar(Point location, int height, int span)
        {
            span -= height;
            rect = new Rectangle(location, new Size(20, height));
            up = new Rectangle(location, new Size(20, 20));
            down = new Rectangle(location.X, location.Y + height - 20, 20, 20);
            offset = 0;
            SetSpan(span);

            upLines = new Point[3];
            upLines[0] = new Point(up.Left + 2, up.Bottom - 2);
            upLines[1] = new Point(up.Left + 10, up.Top + 4);
            upLines[2] = new Point(up.Right - 2, up.Bottom - 2);
            downLines = new Point[3];
            downLines[0] = new Point(down.Left + 2, down.Top + 2);
            downLines[1] = new Point(down.Left + 10, down.Bottom - 4);
            downLines[2] = new Point(down.Right - 2, down.Top + 2);
        }

        public void SetSpan(int span)
        {
            Console.WriteLine(span);
            max = span;
            int height = rect.Height - 40;
            ratio = (float)height / (float)span;
            bar = new Rectangle(rect.X, rect.Y + 20, 20, (int)(ratio * height));
        }

        public void Draw(Graphics g)
        {
            g.FillRectangle(Brushes.Snow, rect);
            g.DrawImage(Resources.up, up);
            g.DrawImage(Resources.down, down);
            Drawing.FillOutlineRectangle(g, bar, Brushes.Silver, Pens.LightGray);
            //g.DrawImage(Resources.bar, bar);
            g.DrawImage(Resources.bar, new Rectangle(bar.X,bar.Y-2,bar.Width,2), new Rectangle(0, 0, 20, 2), GraphicsUnit.Pixel);
            g.DrawImage(Resources.bar, bar, new Rectangle(0, 2, 20, 16), GraphicsUnit.Pixel);
            g.DrawImage(Resources.bar, new Rectangle(bar.X, bar.Bottom, bar.Width, 2), new Rectangle(0, 18, 20, 2), GraphicsUnit.Pixel);
        }

        public bool Click(Point c, int type)
        {
            switch (type)
            {
                case 0: //MouseDown
                    if (bar.Contains(c))
                    {
                        drag = true;
                    }
                    break;
                case 1: //MouseMove
                    if (drag)
                    {
                        int height = rect.Height - 40;
                        int y = c.Y;
                        if (y < up.Bottom) y = up.Bottom;
                        if (y > down.Top - bar.Height) y = down.Top - bar.Height;
                        bar.Y = y;
                        offset = (int)((y-up.Bottom)/ratio);
                        return true;
                    }
                    break;
                case 2: //MouseUp
                    drag = false;
                    if (up.Contains(c) && offset > 0)
                    {
                        offset -= 20;
                        if (offset < 0) offset = 0;
                        bar.Y = (int)(ratio * offset) + up.Bottom;
                        return true;
                    }
                    if (down.Contains(c) && offset < max)
                    {
                        offset += 20;
                        if (offset > max) offset = max;
                        bar.Y = (int)(ratio * offset) + up.Bottom;
                        return true;
                    }
                    break;
            }
            return false;
        }

        internal Rectangle GetRect()
        {
            throw new NotImplementedException();
        }

        public void Move(int y)
        {
            offset += y;
            if (offset > max) offset = max;
            if (offset < 0) offset = 0;
            bar.Y = (int)(offset * ratio + up.Bottom);
            if (bar.Bottom > down.Top) bar.Y = down.Top-bar.Height;
        }
    }
}
